import os
from PySide6.QtWidgets import QDialog,QMessageBox,QVBoxLayout
from views.showpdf.showpdfdialog import Ui_showpdfdialog
from PySide6.QtPdf import QPdfDocument
from PySide6.QtPdfWidgets import QPdfView
from PySide6.QtCore import QPointF

class ShoePDFView(QDialog,Ui_showpdfdialog):
    def __init__(self,file_path):
        super().__init__()
        self.setupUi(self)
        # 文件加载
        self.pdf_document = QPdfDocument(self)
        self.load_pdf_file(file_path)
        # 显示实例化
        self.pdf_view = QPdfView()
        # 设置多页模式
        self.pdf_view.setPageMode(QPdfView.PageMode.MultiPage)
        # pdf数据加载到pdf显示里
        self.pdf_view.setDocument(self.pdf_document)
        # 设置初始缩放模式
        self.pdf_view.setZoomMode(QPdfView.ZoomMode.FitToWidth)

        # 2. 为 frame 创建垂直布局
        frame_layout = QVBoxLayout()
        self.showpdfframe.setLayout(frame_layout)
        frame_layout.addWidget(self.pdf_view)

        self.zoom_in_btn.clicked.connect(self.zoom_in)
        self.zoom_out_btn.clicked.connect(self.zoom_out)
        self.fit_width_btn.clicked.connect(lambda: self.set_zoom_mode(QPdfView.ZoomMode.FitToWidth))
        self.fit_page_btn.clicked.connect(lambda: self.set_zoom_mode(QPdfView.ZoomMode.FitInView))
        self.actual_size_btn.clicked.connect(self.actual_size)
        self.next_page_btn.clicked.connect(self.next_page)
        self.prev_page_btn.clicked.connect(self.pre_page)
        self.start_page.editingFinished.connect(self.jump_page)
        self.setPageName()


    def load_pdf_file(self, file_path):
        """加载 PDF 文件"""
        if not os.path.exists(file_path):
            QMessageBox.warning(self, "错误", f"文件不存在: {file_path}")
            return

        # 加载 PDF 文档
        self.pdf_document.load(file_path)

        # 更新窗口标题
        file_name = os.path.basename(file_path)
        self.setWindowTitle(f"PDF 查看器 - {file_name}")

    def zoom_in(self):
        """放大视图"""
        current_zoom = self.pdf_view.zoomFactor()
        self.pdf_view.setZoomFactor(current_zoom * 1.2)
        self.pdf_view.setZoomMode(QPdfView.ZoomMode.Custom)

    def zoom_out(self):
        """缩小视图"""
        current_zoom = self.pdf_view.zoomFactor()
        self.pdf_view.setZoomFactor(current_zoom / 1.2)
        self.pdf_view.setZoomMode(QPdfView.ZoomMode.Custom)

    def actual_size(self):
        """实际大小"""
        self.pdf_view.setZoomFactor(1.0)
        self.pdf_view.setZoomMode(QPdfView.ZoomMode.Custom)

    def set_zoom_mode(self, mode):
        """设置缩放模式"""
        self.pdf_view.setZoomMode(mode)

    def next_page(self):
        """下一页"""
        navigator = self.pdf_view.pageNavigator()
        if navigator:
            current_page = navigator.currentPage()
            if current_page < self.pdf_document.pageCount() - 1:
                navigator.jump(current_page+1, QPointF(), self.pdf_view.zoomFactor())
            self.setPageName()

    def pre_page(self):
        """上一页"""
        navigator = self.pdf_view.pageNavigator()
        if navigator:
            current_page = navigator.currentPage()
            if current_page < self.pdf_document.pageCount() - 1:
                navigator.jump(current_page - 1, QPointF(), self.pdf_view.zoomFactor())
            self.setPageName()

    def setPageName(self):
        self.end_page.setText(str(self.pdf_document.pageCount()))
        navigator = self.pdf_view.pageNavigator()
        current_page = navigator.currentPage()
        self.start_page.setValue(current_page+1)

    def jump_page(self):
        input_page = int(self.start_page.text().strip())
        navigator = self.pdf_view.pageNavigator()
        if navigator and 0 <= input_page - 1 < self.pdf_document.pageCount():
            navigator.jump(input_page-1,QPointF(), self.pdf_view.zoomFactor())
            self.setPageName()